package com.weixin.servlet.information;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.weixin.service.IUserService;
import com.weixin.service.impl.UserServiceImpl;
import com.weixin.util.PageUtil;
import com.weixin.vo.Notice;
import com.weixin.vo.User;

/**
 * Servlet implementation class UserServlet
 */
/**
 * @author lenovo
 *
 */
@WebServlet("/UserServlet/*")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	IUserService userService = new UserServiceImpl();


	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String pathInfo = request.getPathInfo().substring(1);
		if ("listAll".equals(pathInfo)) {
			listAll(request, response);				//��ѯ��������
		}else if("removeById".equals(pathInfo)) {
			removeById(request,response);			//������Ϣɾ������
		}else if("findAllBySplit".equals(pathInfo)) {
			findAllBySplit(request,response);		//��ҳ��ѯ����
		}else if("findTuijian".equals(pathInfo)) {
			findTuijian(request,response);
		}else if("add".equals(pathInfo)) {
			add(request,response);
		}else if("preEdit".equals(pathInfo)) {
			preEdit(request,response);
		}else if("edit".equals(pathInfo)) {
			edit(request,response);
		}
	} 
	
	/**
	 * ������ѯ���������޸ķ���
	 * @param request
	 * @param response
	 */
	private void edit(HttpServletRequest request, HttpServletResponse response) {
		//���ñ����ʽ
		try {
			request.setCharacterEncoding("utf-8");
			//��jsp�õ�ֵ
			String user_tuijian = request.getParameter("user_tuijian");
			String user_name = request.getParameter("user_name");
			String user_id = request.getParameter("user_id");
			//����ֵ
			User vo = new User();
			vo.setUser_tuijian(user_tuijian);
			vo.setUser_name(user_name);
			vo.setUser_time(new Date());
			vo.setUser_id(user_id);
			
		/*	User vo1 = new User();
			vo1.setUser_tuijian(user_tuijian);
			vo1.setUser_name(user_name);
			vo1.setUser_time(new Date());
			vo1.setUser_id(user_id);
			*/
			//�ض�������ҳ����
			if(userService.edit(vo)) {
				response.sendRedirect(request.getContextPath()+"/UserServlet/findAllBySplit?pageNum=1");
			}else {
				
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * ������ѯҳ����޸ĵ�����Ԥ�޸ķ���
	 * @param request
	 * @param response
	 */
	private void preEdit(HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int user_id = 0;
		if(request.getParameter("user_id")!=null&&!(request.getParameter("user_id").equals(""))) {
			user_id = Integer.valueOf(request.getParameter("user_id"));
		}
		try {
			//���ݹ���id�ҵ�������Ϣ
			User user = userService.findById(user_id);
			
			if(user!=null) {
				response.getWriter().println("["+new Gson().toJson(user)+"]");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * ������ѯģ�����ӷ���
	 * @param request
	 * @param response
	 */
	private void add(HttpServletRequest request, HttpServletResponse response) {
		try {
		//���ñ����ʽ
		request.setCharacterEncoding("utf-8");
		//��jsp�õ�������ֵ
		String user_tuijian = request.getParameter("user_tuijian");
		String user_name = request.getParameter("user_name");
		User vo = new User();
		//����
		vo.setUser_tuijian(user_tuijian);
		vo.setUser_name(user_name);
		vo.setUser_time(new Date());
		vo.setUser_money(vo.getUser_money()+800);

		//�ض�����findAllBySplit����
		response.sendRedirect(request.getContextPath()+"/UserServlet/findAllBySplit");
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

	

	/**
	 * ������ѯҳ��Ĳ�ѯ����
	 * @param request
	 * @param response
	 */
	private void findTuijian(HttpServletRequest request, HttpServletResponse response) {
		Map<String, String[]> parameterMap = request.getParameterMap();
			int pageNum = 1;
	        if(request.getParameter("pageNum")!=null){
	           pageNum = Integer.valueOf(request.getParameter("pageNum"));
	        }
		try {
			List<User> userList = userService.findTuijian(pageNum, PageUtil.PAGESIZE, parameterMap);
	/*	//��ѯ�������˺�������
            int total = userService.findTotalTuijian(parameterMap);
            //������ҳ����
            int pageCount = (int)Math.ceil(total*1.0/PageUtil.PAGESIZE);*/

            request.setAttribute("userList",userList);
      /*     request.setAttribute("pageCount",pageCount);
            request.setAttribute("pageNum",pageNum);*/
            //�����
            request.setAttribute("parameterMap",parameterMap);
            //ת����jsp����ҳ��
			request.getRequestDispatcher("/pages/information/rewardQuery_list.jsp").forward(request, response);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * user���ҳ��ѯ
	 * @param request
	 * @param response
	 */
	private void findAllBySplit(HttpServletRequest request, HttpServletResponse response) {
		
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		try {
			//��÷�ҳ����
			List<User> userList = userService.findAllBySplit(pageNum, PageUtil.PAGESIZE);
			//���������
			int total = userService.findTotal();
			//������ҳ��
			int pageCount = (int) Math.ceil(total*1.0/PageUtil.PAGESIZE);
			//request��������Գ�ԱList
			request.setAttribute("userList",userList);
			request.setAttribute("pageCount", pageCount);
			request.setAttribute("pageNum", pageNum);
			//ת��������ҳ��
			request.getRequestDispatcher("/pages/information/rewardQuery_list.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * ������ѯɾ��
	 * @param request
	 * @param response
	 */
	private void removeById(HttpServletRequest request, HttpServletResponse response) {
		String[] users = request.getParameterValues("user_id");
		
		//ɾ������
		try {
			if(userService.removeById(users)) {
				response.getWriter().println(1);
			}else {
				response.getWriter().println(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void listAll(HttpServletRequest request, HttpServletResponse response) {
		try {
			//����ҵ�������õ����ж���
			List<User> users = userService.selectRecommend();
			//request��������Գ�ԱList
			request.setAttribute("userList",users);
			//ת��������ҳ��
			request.getRequestDispatcher("/pages/information/rewardQuery_list.jsp").forward(request,response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
